package edu.berkeley.guir.webquilt.proxy;

import com.arthurdo.parser.*;
import edu.berkeley.guir.webquilt.*;

/**
 * Edits hyperlink tags and form tags
 *  (e.g. "go" and "a href" tags)
 *
 * <P>
 * This software is distributed under the
 * <A HREF="http://guir.cs.berkeley.edu/projects/COPYRIGHT.txt">
 * Berkeley Software License</A>.
 *
 * <PRE>
 * Revisions:  - WebQuilt-v1.0-2.0.1, Jan 14 2002, kaow
 *               Created class based on HTMLLinkEditor
 *             - WebQuilt Apr 15 2002, kaow
 *               Added support for interdeck WAP logging
 * </PRE>
 *
 * @author  Wayne Kao (
 *          <A HREF="mailto:kaow@hkn.eecs.berkeley.edu">kaow@cs.berkeley.edu</A>
 *          )
 * @since   JDK 1.3
 * @version WebQuilt-v1.0-2.0.1, Jan 14 2002
 */
class WMLGoLinkEditor extends WMLAbstractEditor implements TagEditor {

    public static int curLink = 0;
    public static String url = "";

    public String editTag(WMLTag tag) {

    	// first proxy the HREF attribute

        String inner_begin = "testpages/wq_interdeck.wmls#go_inner('";
        String inner_mid1 = "', ";
        String inner_mid2 = ", '";
        String inner_end = "')";
        String outer_begin = "testpages/wq_interdeck.wmls#go_outer('";
        String outer_mid = "', ";
        String outer_end = ")";

    	String strUrl;
    	boolean secure = entry.secure;

    	curLink++;

    	strUrl = tag.getParam(tag.P_HREF);

        // interdeck card
        if (strUrl.trim().charAt(0) == '#') {
            strUrl = inner_begin + strUrl.trim() + inner_mid1 + curLink + inner_mid2 + url + inner_end;

        // foreign deck
        } else {
    	    if (WebQuiltLib.urlIsSpecial(strUrl))
        	    return tag.toString();

        	strUrl = WebQuiltLib.generateAbsoluteUrl(entry.strBase, strUrl);
        	strUrl = proxy.encodedUrl(strUrl, entry.nextTID, curLink, secure);
            strUrl = outer_begin + strUrl.trim() + outer_mid + curLink + outer_end;
        }

    	tag.setParam(tag.P_HREF, strUrl);



    	// next add hidden fields if it's a form

    	String strMethod = tag.getParam(tag.P_METHOD);

    	// if not a form => return
    	if (strMethod == null || (!strMethod.equalsIgnoreCase("post") && !strMethod.equalsIgnoreCase("get")))
    		return tag.toString();


	    String strNew = tag.toString() + "\n";

    	// added "HIDDEN" form items to house webquilt vars
    	// new replace node
    	strNew += "<postfield " +
    	          "name=\""  + proxy.PARAM_REPLACE + "\" " +
    	          "value=\"" + strUrl + "\">\n";

    	// new tid node
    	strNew  += "<postfield " +
    	           "name=\""  + proxy.PARAM_TID + "\" " +
    	           "value=\"" + String.valueOf(entry.nextTID) + "\">\n";




    	return strNew;

        } //

        public void reset() {
        	curLink = 0;
        } //

} // of class
//==============================================================================

/*
Copyright (c) 2001 Regents of the University of California.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:

      This product includes software developed by the Group for User
      Interface Research at the University of California at Berkeley.

4. The name of the University may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
